< previous page page_182 next page >

Page 182
A Second Approach
This suggests a first solution to this puzzle's challenge for making the GetKeyInfo function more efficient. Find those parameters that are not applicable to the task at hand and change their declarations to ByVal As Long so they can accept NULL parameters. You can take advantage of the Alias command's ability to create a new declaration that calls the RegQueryInfoKey API. The following code declares a new RegQueryInfoKeyV2 function, which is designed to allow you to pass NULL parameters to all of the parameters except lpcSubKeys and lpcbMaxSubKeyLen:
Private Declare Function RegQueryInfoKeyV2 Lib "advapi32.dll" _
Alias "RegQueryInfoKeyA" (ByVal hKey As Long, ByVal lpClass As String, _
ByVal lpcbClass As Long, ByVal lpReserved As Long, lpcSubKeys As Long, _
lpcbMaxSubKeyLen As Long, ByVal lpcbMaxClassLen As Long, ByVal _
lpcValues As Long, ByVal lpcbMaxValueNameLen As Long, ByVal _
lpcbMaxValueLen As Long, ByVal lpcbSecurityDescriptor As Long, _
ByVal lpftLastWriteTime As Long) As Long
The impact on the GetKeyInfo function is impressive, as shown in the version below, named GetKeyInfo2:
  Private Function GetKeyInfo2(ByVal hKey As Long, NumberOfKeys As _
  Long, MaxKeyNameLength As Long) As Long
    Dim res As Long
    res = RegQueryInfoKeyV2(hKey, vbNullString, 0, 0, _
    NumberOfKeys, MaxKeyNameLength, 0, 0, 0, 0, 0, 0)
    If res <> 0 Then
      MsgBox "RegQueryInfoKey error: " & GetErrorString(res)
    End If
    GetKeyInfo2 = res
  End Function
A Third Approach
The GetKeyInfo2 approach suffers from one small disadvantage: Every time you need to access a different subset of information from the RegQueryInfoKey function, you need to create a new alias. Is there a way to keep the efficiency of this approach using a single RegQueryInfoKey declaration?
Yes, you can do it by declaring parameters As Any, in which case the calling code would contain the term ByVal 0& to pass a NULL value. However, the As Any data type is dangerous and should be avoided whenever possible. In this

 
< previous page page_182 next page >